<template>
  <page-wrap back hide-tab :title="personDetail.name" stay-still>
    <view class="info-wrapper">
      <view class="user-info">
        <view class="header">
          <view class="left">
            <text class="username">{{ personDetail.iname }}</text>
            <!-- <text :class="[
                'identity',
                personDetail.rctypDesc === '学生'
                  ? 'is-student'
                  : 'is-not-student',
              ]">{{ personDetail.rctypDesc || "" }}</text> -->
          </view>
          <view class="right">
            <image
              class="avatar"
              :src="personDetail.pic || defaultAvatar"
            ></image>
            <image
              class="sex"
              :src="personDetail.sex == '男' ? male : female"
            ></image>
          </view>
        </view>
        <view class="detail">
          <view class="item">
            <view class="spe" v-if="personDetail.nianling"
              >{{ personDetail.nianling || '' }}岁</view
            >
            <view class="spe" v-if="personDetail.gznum"
              >{{ personDetail.gznum || '' }}年工作经验</view
            >
            <view class="spe" v-if="personDetail.eduDesc">{{
              personDetail.eduDesc || ''
            }}</view>
            <view>{{ personDetail.regionName || '' }}</view>
          </view>
          <view class="item">
            <image class="school" :src="educationHead"></image>
            <text
              >{{ personDetail.school || ''
              }}{{ personDetail.zye ? ' · ' + personDetail.zye : '' }}</text
            >
          </view>
        </view>
      </view>
      <view class="career-report">
        <view class="header">职途专区</view>
        <view class="report-list">
          <view class="item" v-if="showRightFn('resume')">
            <view class="item-header">
              <text class="index sort1"></text>
              <text class="type">简历附件</text>
            </view>
            <view
              class="content"
              v-if="personRight.resume && personRight.resume.length"
            >
              <view
                class="content-item"
                v-for="item in personRight.resume"
                :key="item.id"
              >
                <view class="file-box">
                  <image class="file-img" :src="file"></image>
                  <view>
                    <view class="file-name">{{
                      item.attName || `${personDetail.iname}的简历`
                    }}</view>
                    <view class="file-time">{{ item.updateTime }}上传</view>
                  </view>
                  <view class="file-check" @click="handleCheckResume(item)"
                    >查看简历</view
                  >
                </view>
              </view>
            </view>
            <view class="not-finish" v-else> 未上传 </view>
          </view>
          <view class="item" v-if="showRightFn('personalityQuiz')">
            <view class="item-header">
              <text class="index sort2"></text>
              <text class="type">性格测试</text>
            </view>
            <view class="content" v-if="personRight.personalityQuiz">
              <view class="content-item">
                <view>
                  <text class="desc-name">性格特质：</text>
                  <text class="desc-value">{{
                    getDescName(personRight.personalityQuiz.extra)
                  }}</text>
                </view>
                <view class="operate" @click="gotoDisc">
                  {{ getDescBtnName(personRight.personalityQuiz.extra) }}</view
                >
              </view>
            </view>
            <view class="not-finish" v-else> 未测试 </view>
          </view>
          <view class="item" v-if="showRightFn('resumeDiagnose')">
            <view class="item-header">
              <text class="index sort2"></text>
              <text class="type">简历诊断</text>
            </view>
            <view class="content" v-if="personRight.resumeDiagnose">
              <view class="content-item">
                <view>
                  <text class="desc-name">服务状态：</text>
                  <text class="desc-value">{{
                    getServiceName(personRight.resumeDiagnose.extra)
                  }}</text>
                </view>
                <view class="operate" @click="handleCheckResumeDiagnose">
                  查看结果
                </view>
              </view>
              <template
                v-if="personRight.resumeDiagnose.serviceFileRecordList.length"
              >
                <view
                  class="content-item"
                  v-for="(item, index) in personRight.resumeDiagnose
                    .serviceFileRecordList"
                  :key="index"
                >
                  <view>
                    <text
                      class="desc-name"
                      v-if="
                        personRight.resumeDiagnose.serviceFileRecordList
                          .length > 1
                      "
                      >顾问简历诊断分析报告{{ index + 1 }}</text
                    >
                    <text class="desc-name" v-else>顾问简历诊断分析报告</text>
                  </view>
                  <view
                    class="operate"
                    @click="handleServiceFileRecordList(item)"
                  >
                    查看报告
                  </view>
                </view>
              </template>
            </view>
            <view class="not-finish" v-else> 未诊断 </view>
          </view>
          <view class="item" v-if="showRightFn('careerQuiz')">
            <view class="item-header">
              <text class="index sort2"></text>
              <text class="type">职业测评</text>
            </view>
            <view class="content" v-if="personRight.careerQuiz">
              <view class="content-item">
                <view>
                  <text class="desc-value">职业守护兽：</text>
                  <text class="desc-value">{{
                    getAnimalName(personRight.careerQuiz.extra)
                  }}</text>
                </view>
                <view class="operate" @click="handleCheckResultResume">
                  {{ getAnimalNtnName(personRight.careerQuiz.extra) }}
                </view>
              </view>
            </view>
            <view class="not-finish" v-else> 未测评 </view>
          </view>
          <view class="item" v-if="showRightFn('careerReport')">
            <view class="item-header">
              <text class="index sort2"></text>
              <text class="type">职业报告</text>
            </view>
            <view
              v-if="
                personRight.careerReport &&
                personRight.careerReport.report.length
              "
              class="content"
            >
              <view
                class="content-item"
                v-for="(item, index) in personRight.careerReport.report"
                :key="index"
              >
                <view class="desc-file">
                  {{ item.positionName }}
                </view>
                <view class="operate" @click="handleCheckCareerReport(item)">
                  查看报告
                </view>
              </view>
            </view>
            <view class="not-finish" v-else> 未完成 </view>
          </view>
          <view class="item" v-if="showRightFn('aiInterview')">
            <view class="item-header">
              <text class="index sort2"></text>
              <text class="type">AI面评</text>
            </view>
            <view class="content" v-if="personRight.aiInterview">
              <view class="content-item">
                <view>
                  <text class="desc-name">面评结果</text>
                </view>
                <view
                  class="operate"
                  @click="handleGotoAIResult(personRight.aiInterview)"
                >
                  查看结果
                </view>
              </view>
            </view>
            <view class="not-finish" v-else> 未完成 </view>
          </view>
          <view class="item" v-if="showRightFn('ztReport')">
            <view class="item-header">
              <text class="index sort2"></text>
              <text class="type">职途报告</text>
            </view>
            <view class="content" v-if="personRight.ztReport">
              <view class="content-item">
                <view>
                  <text class="desc-name">职途报告</text>
                </view>
                <view
                  class="operate"
                  @click="handleGotoZTReport(personRight.ztReport)"
                >
                  查看报告
                </view>
              </view>
            </view>
            <view class="not-finish" v-else> 未完成 </view>
          </view>
        </view>
      </view>
    </view>
  </page-wrap>
</template>

<script>
import { env } from '@/api/root';
import { personInfo, getPersonRightNum } from '@/api/modules/career';
export default {
  data() {
    return {
      defaultAvatar: this.$imgBase + 'common/avatar_default.png',
      male: this.$imgBase + 'career/careerAssessResult/male.png',
      female: this.$imgBase + 'career/careerAssessResult/female.png',
      file: this.$imgBase + 'career/careerAssessResult/file.png',
      personId: '',
      educationHead:
        this.$imgBase + 'career/careerAssessResult/educationHead.png',
      personDetail: {
        iname: '',
        pic: '',
        gznum: '',
        nianling: '',
        sex: '',
        school: '',
        job: '',
        rctypDesc: '',
        regionName: '',
        native: '',
        grzz: '',
        eduDesc: ''
      },
      personRight: null,
      rightIdList: [],
      rightKeys: [],
      assessmentVersion: uni.getStorageSync('assessmentVersion'),
      cardType: uni.getStorageSync('cardType')
    };
  },
  onLoad(opt) {
    this.personId = opt.personId || '';
    this.cardType = uni.getStorageSync('cardType');
  },
  onShow() {
    this.personId && this.personInfoFn();
  },
  mounted() {
    this.personInfoFn();
  },
  methods: {
    async getPersonRightNumFn(rightId) {
      const params = {
        personId: this.personId || undefined,
        rightId,
        cardType: this.cardType
      };
      const res = await getPersonRightNum(params);
      return res.data.ownNum - res.data.useNum;
    },
    showRightFn(key) {
      return this.rightKeys.indexOf(key) > -1;
    },
    handleCheckCareerReport(item) {
      const fileUrl = item.pdfUrl;
      const fileName = item.positionName + '.pdf';
      this.openReport(fileUrl, fileName, '打开职业报告出错！');
    },
    handleServiceFileRecordList(item) {
      const fileUrl = item.fileUrl;
      const fileName = item.fileName;
      this.openReport(fileUrl, fileName, '打开诊断报告出错！');
    },
    //打开职业报告
    openReport(fileUrl, fileName, message) {
      if (!fileUrl) {
        uni.showToast({ icon: 'none', title: message });
        return;
      }
      uni.showLoading({
        title: '打开中'
      });

      uni.downloadFile({
        url: fileUrl, // 文件的本身url
        filePath: wx.env.USER_DATA_PATH + '/' + fileName, // 本地自定义的文件名
        success: function (res) {
          let filePath = res.filePath;
          uni.openDocument({
            filePath: filePath,
            success: function () {
              uni.hideLoading();
            },
            fail: function (err) {
              uni.hideLoading();
            }
          });
          uni.hideLoading();
        },
        fail: function (res) {
          uni.hideLoading();
          uni.showToast({
            icon: 'none',
            title: '链接已经过期！',
            duration: 2000
          });
        },
        complete: (res) => {
          console.log('complete', res);
        }
      });
    },
    handleGotoAIResult(item) {
      uni.navigateTo({
        url: `/pagesSub/companyCenter/interviewAI/majorSearchAI?rightPersonId=${item.personId}`
      });
    },
    getAnimalName(val) {
      const extra = JSON.parse(val);
      if (extra && extra.character) {
        return extra.character;
      } else {
        return '未知';
      }
    },
    getAnimalNtnName(val) {
      const extra = JSON.parse(val);
      if (extra && extra.character) {
        return '查看结果';
      } else {
        return '立即测试';
      }
    },
    getDescName(val) {
      const extra = JSON.parse(val);
      if (extra && extra.character) {
        return extra.character;
      } else {
        return '未测试';
      }
    },
    getDescBtnName(val) {
      const extra = JSON.parse(val);
      if (extra && extra.character) {
        return '查看结果';
      } else {
        return '立即测试';
      }
    },
    getServiceName(val) {
      const extra = JSON.parse(val);
      if (extra && extra.diagnoseRecordId) {
        return '已使用';
      } else {
        return '未使用';
      }
    },
    personInfoFn() {
      const params = {
        personId: this.personId
      };
      personInfo(params).then((res) => {
        if (res.code == 200) {
          // 删除职途v1.0版本的权益，不展示
          if (res.data.personRight.CompetAnalysis) {
            delete res.data.personRight.CompetAnalysis;
          }
          if (res.data.personRight.onlineCounsele) {
            delete res.data.personRight.onlineCounsele;
          }
          this.personDetail = res.data.personDetail;
          this.personRight = res.data.personRight;
          this.rightKeys = Object.keys(this.personRight);
          this.rightIdList = res.data.rightIdList;
        } else {
          uni.showToast({ icon: '', title: res.status_desc, duration: 2000 });
        }
      });
    },
    handleCheckResume(item) {
      uni.showLoading({
        title: '正在打开简历'
      });
      uni.downloadFile({
        url: item.attUrl,
        success: (res) => {
          if (+res.statusCode === 200) {
            uni.saveFile({
              tempFilePath: res.tempFilePath,
              success: function (saveRes) {
                setTimeout(() => {
                  uni.hideLoading();
                  uni.openDocument({
                    filePath: saveRes.savedFilePath
                  });
                }, 1500);
              }
            });
          }
        }
      });
    },
    handleGotoZTReport(item) {
      if (!item.extra) {
        uni.showToast({
          icon: 'none',
          title: '职途报告暂未生成',
          duration: 1000
        });
        return;
      }
      const extra = JSON.parse(item.extra);
      uni.showLoading({
        title: '正在打开报告'
      });
      uni.downloadFile({
        url: extra.pdfUrl || extra.pdfUrlV2,
        success: (res) => {
          if (+res.statusCode === 200) {
            uni.saveFile({
              tempFilePath: res.tempFilePath,
              success: function (saveRes) {
                setTimeout(() => {
                  uni.hideLoading();
                  uni.openDocument({
                    filePath: saveRes.savedFilePath
                  });
                }, 1500);
              },
              fail: function (err) {
                console.log('err', err);
                uni.showToast({ icon: 'none', title: '打开职途报告失败' });
                uni.hideLoading();
              }
            });
          }
        }
      });
    },
    async gotoDisc() {
      const discLeftUseNum = await this.getPersonRightNumFn(1);
      const extra = JSON.parse(this.personRight.personalityQuiz.extra);
      let url;
      if (extra && extra.character) {
        if (env == 'test') {
          url = `https://3gtest.crm1001.com/disc/#/disc/result?userId=${extra.userId}&iname=${this.personDetail.iname}&phone=18825449087&from=careerPathResult&discLeftUseNum=${discLeftUseNum}&cardType=${this.cardType}`;
        } else if (env == 'pro' || env == 'pre') {
          url = `https://3g.crm1001.com/v2/disc/#/disc/result?userId=${extra.userId}&iname=${this.personDetail.iname}&phone=18825449087&from=careerPathResult&discLeftUseNum=${discLeftUseNum}&cardType=${this.cardType}`;
        }

        uni.navigateTo({
          url: '/pagesSub/public/web/index?link=' + encodeURIComponent(url)
        });
      } else {
        if (getApp().globalData.userIsConsultant) {
          uni.showToast({ icon: '', title: '用户还没进行测试' });
          return;
        }
        if (env == 'test') {
          url = `https://3gtest.crm1001.com/disc/#/disc/question?personId=${this.personId}&iname=${this.personDetail.iname}&phone=18825449087&from=careerPathResult&discLeftUseNum=${discLeftUseNum}&cardType=${this.cardType}`;
        } else if (env == 'pro' || env == 'pre') {
          url = `https://3g.crm1001.com/v2/disc/#/disc/question?personId=${this.personId}&iname=${this.personDetail.iname}&phone=18825449087&from=careerPathResult&discLeftUseNum=${discLeftUseNum}&cardType=${this.cardType}`;
        }

        uni.navigateTo({
          url: '/pagesSub/public/web/index?link=' + encodeURIComponent(url)
        });
      }
    },
    handleCheckResultResume() {
      const extra = JSON.parse(this.personRight.careerQuiz.extra);
      if (extra && extra.character) {
        uni.navigateTo({
          url: `/pagesSub/careerAssessResultActNew/index?personId=${this.personId}`
        });
        // character 有值，就跳去结果页面
        // if (!this.assessmentVersion || this.assessmentVersion == '1') {
        //   uni.navigateTo({
        //     url: `/pagesSub/career/activity/CareerAssessResultAct?personId=${this.personId}`
        //   });
        // } else if (this.assessmentVersion == '2') {
        //   uni.navigateTo({
        //     url: `/pagesSub/careerAssessResultActNew/index?personId=${this.personId}`
        //   });
        // }
      } else {
        uni.navigateTo({
          url: `/pagesSub/career/activity/vocationalAssessment?personId=${this.personId}&fromWhere=careerPath` // 还没测评 //
        });
      }
    },
    handleCheckResumeDiagnose() {
      const extra = JSON.parse(this.personRight.resumeDiagnose.extra);
      if (extra && extra.capacityId) {
        uni.navigateTo({
          url: `/pagesSub/newResume/resumeAssessment/index?target=AIResumeDiagnoseNew`
        });
      } else {
        uni.showToast({
          title: '请先进行简历诊断',
          duration: 2000,
          icon: 'none'
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.info-wrapper {
  padding: 24rpx;
  .user-info {
    border-radius: 30rpx;
    padding: 24rpx;
    background-color: #fff;
    .header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      background-color: #fff;
      .left {
        display: flex;
        align-items: center;
        .username {
          font-size: 40rpx;
          font-weight: 600;
          margin-right: 6rpx;
        }
        .identity {
          padding: 4rpx 24rpx;
          border-radius: 22rpx;
        }
        .is-student {
          color: #3072ff;
          background-color: #dce4ff;
        }
        .is-not-student {
          color: #0ea23e;
          background: #ccefc8;
        }
      }
      .right {
        position: relative;
        .avatar {
          width: 80rpx;
          height: 80rpx;
          border-radius: 50%;
        }
        .sex {
          width: 28rpx;
          height: 28rpx;
          border-radius: 50%;
          position: absolute;
          right: -12rpx;
          top: -2rpx;
        }
      }
    }
    .detail {
      background-color: #fff;
      font-size: 26rpx;
      .item {
        display: flex;
        padding: 16rpx 0;
        color: #555555;
        .spe {
          border-right: 1px solid #ddd;
          margin-right: 20rpx;
          padding-right: 20rpx;
        }
        .school {
          width: 36rpx;
          height: 36rpx;
          margin-right: 16rpx;
        }
      }
    }
    .assessment {
      border-top: 1px solid #f5f5f5;
      background-color: #fff;
      .title {
        padding: 16rpx 0;
        font-size: 32rpx;
        font-weight: 600;
        color: #222222;
      }
    }
  }
  .career-report {
    background-color: #fff;
    border-radius: 16rpx;
    margin-top: 16rpx;
    .header {
      padding: 28rpx 16rpx 32rpx 16rpx;
      font-size: 40rpx;
      font-weight: 600;
      color: #222;
      text-align: center;
      background: linear-gradient(180deg, #fff4ec 0%, #ffffff 100%);
    }
    .report-list {
      padding: 0 16rpx;
      .item {
        padding: 12rpx 0;
        &-header {
          display: flex;
          align-items: center;
          padding: 6rpx 0 12rpx 0;
          .index {
            width: 6rpx;
            height: 28rpx;
            background: #f75a3e;
            border-radius: 3rpx;
          }
          .type {
            padding-left: 20rpx;
            font-size: 32rpx;
            font-weight: 600;
            color: #222222;
          }
        }
        .content {
          &-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 12rpx 0;
            font-size: 28rpx;
            .file-box {
              display: flex;
              align-items: center;
              padding: 32rpx 23rpx;
              width: 100%;
              background-color: #fff7f0;
              border-radius: 10rpx;
              .file-img {
                width: 52rpx;
                height: 66rpx;
                margin-right: 26rpx;
              }
              .file-name {
                max-width: 409rpx;
                text-overflow: ellipsis;
                overflow: hidden;
                white-space: nowrap;
                font-size: 32rpx;
                color: #222222;
              }
              .file-time {
                font-size: 20rpx;
                color: #bfa0a0;
                padding-top: 6rpx;
              }
              .file-check {
                margin-left: auto;
                color: #f75a3e;
                font-size: 28rpx;
              }
            }
            .desc-name {
              color: #585858;
              text-overflow: ellipsis;
              white-space: nowrap;
              overflow: hidden;
            }
            .desc-value {
              color: #555;
              padding-left: 6rpx;
              font-weight: 600;
              max-width: 409rpx;
              text-overflow: ellipsis;
              overflow: hidden;
              white-space: nowrap;
            }
            .desc-file {
              color: #555;
              max-width: 430rpx;
              text-overflow: ellipsis;
              overflow: hidden;
              white-space: nowrap;
            }
            .operate {
              padding: 8rpx 22rpx;
              background: #ffffff;
              border-radius: 32rpx;
              color: #f75a3e;
              border: 1rpx solid #f75a3e;
            }
          }
        }
        .not-finish {
          text-align: left;
          color: #999;
        }
      }
    }
  }
}
</style>
